package com.knight.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class JDBCTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		Connection conn = null;
		PreparedStatement st = null;
		String sql = "select * from test";
		try {
			Class.forName("com.oracle.dirver.OracleDriver");
			conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:test", "user", "pwd");
			conn.setAutoCommit(false);
			st = conn.prepareStatement(sql);
			
			for(int i=1; i<100000; i++){
				st.addBatch();
				if(i%10000 == 0){
					st.executeBatch();
					conn.commit();
				}
			}
			st.executeBatch();
			conn.commit();
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			try {
				closeRs(conn);
				closeRs(st);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

	}
	
	private static void closeRs(Object obj)throws Exception{
		if(null == obj){
			return ;
		}
		
		if(obj instanceof Connection){
			((Connection)obj).close();
		}else if(obj instanceof Statement){
			((Statement)obj).close();
		}else if(obj instanceof ResultSet){
			((ResultSet)obj).close();
		}else{
			throw new Exception("不合法的参数传递!");
		}
	}

}
